package com.example.administrator.yuejian2.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.adapter.ClubAdapter;
import com.example.administrator.yuejian2.adapter.VLayoutClubAdapter;
import com.example.administrator.yuejian2.bean.ClubBean;
import com.example.administrator.yuejian2.model.ClubModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends Fragment {

    private RecyclerView recyclerView;
    private VLayoutClubAdapter vLayoutClubAdapter;
  //  private RecyclerView.LayoutManager layoutManager;
    private RetrofitListener<List<ClubBean>> listener;
    private RefreshLayout refreshLayout;

    public ClubFragment() {
        // Required empty public constructor
    }

    private void initView(View v) {
        recyclerView = v.findViewById(R.id.club_recyclerview);
        refreshLayout = v.findViewById(R.id.refreshClubList);
      /*  layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ClubAdapter(getActivity());
        recyclerView.setAdapter(adapter);*/

        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(getContext());
        recyclerView.setLayoutManager(virtualLayoutManager);
        //设置view缓存个数
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        recyclerView.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        //RecylerView创建适配器，初始化DelegateAdapter
        DelegateAdapter delegateAdapter = new DelegateAdapter(virtualLayoutManager, false);
        recyclerView.setAdapter(delegateAdapter);
        List<DelegateAdapter.Adapter> adapters=new LinkedList<>();
        delegateAdapter.addAdapters(adapters);
       vLayoutClubAdapter=new VLayoutClubAdapter(getContext(),new StaggeredGridLayoutHelper(3,0));
        delegateAdapter.addAdapter(vLayoutClubAdapter);



        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                club();
            }
        });
    }

    private void club() {
        ClubModel activityModel = new ClubModel();
        activityModel.clubList(listener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        listener = new RetrofitListener<List<ClubBean>>() {
            @Override
            public void onSuccess(List<ClubBean> clubBeans, int flag) {
                vLayoutClubAdapter.getClubData(clubBeans);
            }

            @Override
            public void onFail() {

            }
        };
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_club, null);
        initView(view);
        club();
        return view;
    }

}
